#!/usr/bin/env python3

class Menu:
    def __init__(self, **kargs):
        """
        菜单对象初始化
        """
        self.level = 0
        self.items = []
        self.title = '========> 学生成绩管理系统 <========'
        self.quitStr = '退出'
        self.__dict__.update(kargs)

    def add(self, msg, handle=None):
        """
        给菜单添加项
            handle == None      添加退出项
            handle == callable  添加菜单项
            handle == Menu      添加子菜单
        """
        item = {}
        item['msg'] = msg
        item['handle'] = handle
        if isinstance(handle, Menu):
            handle.level = self.level + 1
        self.items.append(item)

        return self

    def showItems(self):
        for index, it in enumerate(self.items):
            if self.level < 1:
                print("========> %s%d. %s\t   <========" % ('    ' * self.level, index + 1, it['msg']))
            else:
                print("\t----> %s%d. %s <----" % ('' * self.level, index + 1, it['msg']))

    def getInput(self, num):
        while True:
            if self.level < 1:
                s = input("%s请输入[1 - %d]: " % ('    ' * self.level, num))
            else:
                s = input("\t%s请输入类型[1 - %d]: " % ('' * self.level, num))
            if not s:
                continue
            n = int(s)
            if 1 <= n <= num:
                return n

    def addQuitItem(self):
        """
        添加退出菜单项
        """
        addFlags = True
        for x in self.items:
            if None in x.values():
                addFlags = False
                break

        if addFlags:
            quitStr = self.quitStr if self.level == 0 else "exit"
            self.add(quitStr)

    def run(self):
        """
        运行菜单程序
        """

        self.addQuitItem()

        itemNum = len(self.items)

        #  循环菜单执行
        while True:
            #  打印菜单标题
            print("%s%s" % ('    ' * self.level, self.title))
            #  输出菜单项
            self.showItems()

            #  获取用户输入
            i = self.getInput(itemNum)

            #  根据用户输入执行菜单项
            handle = self.items[i - 1]['handle']
            if not handle:
                break
            elif isinstance(handle, Menu):
                handle.run()
            else:
                handle(i)

if __name__ == "__main__":
    def g(index):
        if index == 1:
            input("\t请输入学生姓名: ")
        if index == 6:
            print("\t存储成功!")
        elif index == 7:
            print("\t加载成功!")

    def p(index):
        if index == 1:
            input("\t请输入关键词: ")
        if index == 2:
            input("\t请输入关键词: ")
        if index == 3:
            input("\t请输入关键词: ")
        if index == 4:
            input("\t请输入关键词: ")
        if index == 5:
            input("\t请输入关键词: ")

    m = Menu()
    ms = Menu(title='\t    修改菜单选项')
    #  mss = Menu(title='')

    m.add("添加", g).add("修改", ms).add("查询", ms).add("删除", ms).add("遍历", g).add("存储", g).add("加载", g)

    ms.add("name", p)
    ms.add("sex ", p)
    ms.add("id  ", p)
    ms.add("en  ", p)
    ms.add("cn  ", p)

    #  mss.add("", p)

    m.run()
